//
//  WedViewController.m
//  ZAKER
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "WedViewController.h"

@interface WedViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) NSURL *webUrl;
@property (nonatomic, retain) MBProgressHUD *mb;

@end

@implementation WedViewController

-(void)dealloc
{
    
    [_model release];
    [_mb release];
    [_webUrl release];
    
    [super dealloc];
    
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
    self.mb = [[MBProgressHUD alloc]initWithView:window];
    [window addSubview:_mb];
    _mb.labelText = @"正在加载";
    [_mb show:YES];
    [_mb release];
    
    [self getConnection];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        
        if (_mb != nil) {
            [_mb removeFromSuperview];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"数据加载失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert  show];
            [alert release];
        
        }
    });

    [self createWebView];
    NSLog(@"%s", __func__);
    
}

#pragma mark - 数据解析 

- (void)getConnection
{
    
    NSString *string = [NSString stringWithFormat:@"http://api.m.jiemian.com/article/%@.json?version=2.1.1&", self.model.id];
    
    [MyAFNetworking GetWithURL:string dic:nil data:^(id responsder) {
        
        // 因为有第二种情况,所以这个时候dict[@"result"] 返回的是nil,往数组中添加nil是不对的
        if (responsder[@"result"]) {
            
            NSString *myStr = [[responsder objectForKey:@"result"] objectForKey:@"art_url"];
            self.webUrl = [NSURL URLWithString:myStr];
            [self createWebView];
            
        }
        
        _mb.hidden = YES;
        _mb = nil;
        
    }];
}

#pragma mark - 创建webView
-(void)createWebView
{
    
    UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, -50 * myHeight, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:web];
    
    /* 检测文本显示 */
    web.scalesPageToFit = YES;
    web.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:self.webUrl];
    [web loadRequest:request];
    [web release];
    
    [_mb hide:YES];
    _mb = nil;
   
}


- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];

}


@end
